package com.sfac.javaSpringBoot.modules.common.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import javax.persistence.*;
import java.io.Serializable;

import java.time.LocalDateTime;

@MappedSuperclass//它的属性将映射到其他Bean中
public class AbstractEntity implements Serializable{
        @Id//@Id 标注用于声明一个实体类的属性映射为数据库的主键列
        @GeneratedValue(strategy = GenerationType.IDENTITY)//自动增长
        @Column(nullable = false, updatable = false)//不为空，不可以修改
        private Integer id;
        @JsonSerialize(using = LocalDateTimeSerializer.class)//新的时间类
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")//时间格式
        @Column(updatable = false)//不更新该字段
        private LocalDateTime createDate;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public LocalDateTime getCreateDate() {
            return createDate;
        }

        public void setCreateDate(LocalDateTime createDate) {
            this.createDate = createDate;
        }

        @PrePersist//在开始前调用该方法，生成时间
        public void preInsert() {
            setCreateDate(LocalDateTime.now());
        }
}
